package online.westonline.com.westonline.ui;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easy.west.westclient.api.base.bean.SuccessBean;
import com.easy.west.westclient.api.workplan.event.UploadPlanEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.TimePicker;
import online.westonline.com.westonline.MyApplication;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.entry.EventModel;
import online.westonline.com.westonline.utils.ToastUtils;
import online.westonline.com.westonline.utils.UiUtlis;

public class WorkPlanEditActivity extends BaseActivity {


    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;
    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.edit_plan_content)
    EditText editPlanContent;
    @BindView(R.id.li_date)
    LinearLayout liDate;
    @BindView(R.id.tv_selcet_start_time)
    TextView tvSelcetStartTime;
    @BindView(R.id.tv_selcet_end_time)
    TextView tvSelcetEndTime;
    @BindView(R.id.check_top_today)
    CheckBox checkTopToday;
    @BindView(R.id.ra_top_check)
    RelativeLayout raTopCheck;
    @BindView(R.id.check_one_today)
    CheckBox checkOneToday;
    @BindView(R.id.ra_one_check)
    RelativeLayout raOneCheck;
    @BindView(R.id.check_two_today)
    CheckBox checkTwoToday;
    @BindView(R.id.ra_two_check)
    RelativeLayout raTwoCheck;
    @BindView(R.id.check_three_today)
    CheckBox checkThreeToday;
    @BindView(R.id.ra_three_check)
    RelativeLayout raThreeCheck;
    @BindView(R.id.check_four_today)
    CheckBox checkFourToday;
    @BindView(R.id.ra_four_check)
    RelativeLayout raFourCheck;
    @BindView(R.id.tv_selcet_date)
    TextView tvSelcetDate;
    private EventModel plan;
    //日期选择用原生的
    private TimePicker start_timePicker;
    private TimePicker end_timepicker;
    private DatePicker datePicker;
    private String mStartTime;
    private String mEndTime;
    private int now_month_day;
    private int now_month;
    private int now_year;
    private int select_year;
    private int start_hour;
    private int end_hour;
    private int start_minute;
    private int end_minute;
    private int select_month_day;
    private int select_month;
    //能否上传标志
    private boolean canupload;

    @Override
    protected void initview() {
        initcountview();
        inittoobar();
        initsession();
    }

    /**
     * 倒计时设计
     */
    private void initcountview() {
        
    }

    /**
     * 事件初始化
     */
    private void initsession() {
        //打开日期选择器
        liDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker.show();
            }
        });
        //开始时间选择
        tvSelcetStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               start_timePicker.show();
            }
        });
        //结束时间选择
        tvSelcetEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                end_timepicker.show();
            }
        });
        //日期选择器监听
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                select_year = Integer.parseInt(year);
                select_month = Integer.parseInt(month);
                select_month_day = Integer.parseInt(day);
                if (select_year<now_year)
                {
                    ToastUtils.showToast("不能在过去创建计划");
                    return;
                }
                if (select_month==now_month)
                {
                    if (select_month_day<now_month_day)
                    {
                        ToastUtils.showToast("不能在过去创建计划");
                        return;
                    }
                }
                if (select_year==now_year)
                {
                    if (select_month<now_month)
                    {
                        ToastUtils.showToast("不能在过去创建计划");
                        return;
                    }
                }
                tvSelcetDate.setText(select_year+"-"+select_month+"-"+select_month_day);
            }
        });
        end_timepicker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute) {
                //选择结束时间
                if (!tvSelcetDate.getText().equals("点击选择日期"))
                {
                    String hour_str = hour;
                    String minute_str = minute;
                    end_hour = Integer.parseInt(hour);
                    end_minute = Integer.parseInt(minute);
                    //判断是否同一天（不用了）
                    if (end_hour<start_hour)
                    {
                        ToastUtils.showToast("结束时间不能低于开始时间或执行时间太短");
                        return;
                    }
                    if (end_hour==start_hour)
                    {
                        if (end_minute<start_minute||end_minute==start_minute)
                        {
                            ToastUtils.showToast("结束时间不能低于开始时间或执行时间太短");
                            return;
                        }
                    }
                    if (end_hour < 10) {
                        hour_str = "0" + start_hour;
                    }
                    if (end_minute < 10) {
                        minute_str = "0" + start_minute;
                    }
                    tvSelcetEndTime.setText(hour_str+":"+minute_str);
                    mEndTime=hour_str+":"+minute_str;
                }
                else
                {
                    ToastUtils.showToast("请先选择日期");
                }

            }
        });
        //时间选择器的监听
        start_timePicker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute) {
                if (!tvSelcetDate.getText().equals("点击选择日期"))
                {

                        String hour1_str = hour;
                        String minute1_str = minute;
                        //未选择开始时间
                        start_hour = Integer.parseInt(hour);
                        start_minute = Integer.parseInt(minute);
                        if (start_hour < 10) {
                            hour1_str = "0" + start_hour;
                        }
                        if (start_minute < 10) {
                            minute1_str = "0" + start_minute;
                        }
                        tvSelcetStartTime.setText(hour1_str + ":" + minute1_str);
                        mStartTime = hour1_str + ":" + minute1_str;
                }
                else
                {
                    ToastUtils.showToast("请先选择日期");
                }
            }

        });
        //置顶监听
        raTopCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkTopToday.setChecked(!checkTopToday.isChecked());
            }
        });
        //置顶
        checkTopToday.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                plan.setMAllday(isChecked);
                if (isChecked) {
                    checkTwoToday.setChecked(false);
                    checkThreeToday.setChecked(false);
                    checkOneToday.setChecked(false);
                    checkFourToday.setChecked(false);
                    tvSelcetEndTime.setClickable(false);
                    tvSelcetEndTime.setText("全天计划");
                    tvSelcetStartTime.setClickable(false);
                    tvSelcetStartTime.setText("全天计划");
                    mStartTime=null;
                    mEndTime=null;
                    //起始时间应为全天
                    plan.setMStartTime("00:00");
                    plan.setMEndTime("23:59");
                    tvSelcetStartTime.setTextColor(UiUtlis.getColor(R.color.colorMenuBtn));
                    tvSelcetEndTime.setTextColor(UiUtlis.getColor(R.color.colorMenuBtn));
                    canupload = true;

                } else {
                    tvSelcetStartTime.setClickable(true);
                    tvSelcetStartTime.setText("开始时间");
                    tvSelcetEndTime.setClickable(true);
                    tvSelcetEndTime.setText("结束时间");
                    canupload = false;
                }
            }
        });
        //完成
        tvTopbarMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editPlanContent.getText().toString();
                if (TextUtils.isEmpty(editPlanContent.getText()))
                {
                    ToastUtils.showToast("无内容");
                    return;
                }
                if (tvSelcetDate.getText().equals("点击选择日期"))
                {
                    ToastUtils.showToast("未选择日期");
                    return;
                }
                if (
                        checkFourToday.isChecked()==false
                        &&checkOneToday.isChecked()==false
                        &&checkThreeToday.isChecked()==false
                        &&checkTopToday.isChecked()==false
                        &&checkTwoToday.isChecked()==false)
                {
                    if (mStartTime==null||mEndTime==null) {
                        ToastUtils.showToast("请设置时间范围");
                        return;
                    }
                }
                plan.setDate(tvSelcetDate.getText().toString());
                plan.setDay(select_month_day);
                plan.setMAllday(checkTopToday.isChecked());
                if (checkTopToday.isChecked())
                {
                    plan.setMStartTime("00:00");
                    plan.setMEndTime("23:59");
                }
                else
                {
                    plan.setMStartTime(tvSelcetStartTime.getText().toString());
                    plan.setMEndTime(tvSelcetEndTime.getText().toString());
                }
                plan.setMColor(UiUtlis.RandomColor());
                plan.setYear(select_year);
                plan.setMonth(select_month);
                //上传
                MyApplication.getClient().createWorkPlan(
                       plan.getMStartTime(),
                       plan.getMEndTime(),
                        content, plan.getDate(), checkTopToday.isChecked(), 1);
            }
        });
        //上午选择
        raOneCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               checkOneToday.setChecked(!checkOneToday.isChecked());
            }
        });
        //中午选择
        raTwoCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                checkTwoToday.setChecked(!checkTwoToday.isChecked());
            }
        });
        //下午选择
        raThreeCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkThreeToday.setChecked(!checkThreeToday.isChecked());
            }
        });
        raFourCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkFourToday.setChecked(!checkFourToday.isChecked());
            }
        });
        //checkbox监听
        checkOneToday.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setedit(false);
                    //设置为今天
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);
                    plan.setDate(simpleDateFormat.format(System.currentTimeMillis()));
                    //时间9:00~12:00
                    checkThreeToday.setChecked(false);
                    checkTwoToday.setChecked(false);
                    checkTopToday.setChecked(false);
                    checkFourToday.setChecked(false);
                    tvSelcetStartTime.setText("9:00");
                    tvSelcetEndTime.setText("12:00");
                    plan.setMStartTime("9:00");
                    plan.setMEndTime("12:00");
                    canupload = true;

                } else {
                    setedit(true);
                    canupload = false;
                }
            }
        });
        checkTwoToday.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setedit(false);
                    //设置为今天
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);
                    plan.setDate(simpleDateFormat.format(System.currentTimeMillis()));
                    //时间9:00~12:00
                    checkOneToday.setChecked(false);
                    checkThreeToday.setChecked(false);
                    checkTopToday.setChecked(false);
                    checkFourToday.setChecked(false);
                    tvSelcetStartTime.setText("12:00");
                    tvSelcetEndTime.setText("14:00");
                    plan.setMStartTime("12:00");
                    plan.setMEndTime("14:00");
                    canupload = true;

                } else {
                    setedit(true);
                    canupload = false;
                }
            }
        });
        checkThreeToday.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setedit(false);
                    //设置为今天
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);
                    plan.setDate(simpleDateFormat.format(System.currentTimeMillis()));
                    //tvStartDay.setText(simpleDateFormat.format(System.currentTimeMillis()));
                    //时间9:00~12:00
                    checkOneToday.setChecked(false);
                    checkTwoToday.setChecked(false);
                    checkTopToday.setChecked(false);
                    checkFourToday.setChecked(false);
                    tvSelcetStartTime.setText("14:00");
                    tvSelcetEndTime.setText("18:00");
                    plan.setMStartTime("14:00");
                    plan.setMEndTime("18:00");
                    canupload = true;

                } else {
                    setedit(true);
                    canupload = false;
                }
            }
        });
        checkFourToday.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setedit(false);
                    //设置为今天
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);
                    plan.setDate(simpleDateFormat.format(System.currentTimeMillis()));
                    //tvStartDay.setText(simpleDateFormat.format(System.currentTimeMillis()));
                    //时间9:00~12:00
                    checkOneToday.setChecked(false);
                    checkTwoToday.setChecked(false);
                    checkTopToday.setChecked(false);
                    checkThreeToday.setChecked(false);
                    tvSelcetStartTime.setText("19:00");
                    tvSelcetEndTime.setText("23:59");
                    plan.setMStartTime("19:00");
                    plan.setMEndTime("23:59");
                    canupload = true;

                } else {
                    setedit(true);
                    canupload = false;
                }
            }
        });
    }

    /**
     * 设置控件属性
     */
    private void setedit(boolean flag) {
        tvSelcetEndTime.setClickable(flag);
        tvSelcetStartTime.setClickable(flag);
        if (flag)
        {
            tvSelcetStartTime.setText("开始时间");
            tvSelcetEndTime.setText("结束时间");
        }
        mStartTime=null;
        mEndTime=null;
        //全部初始化
        start_hour = 0;
        start_minute = 0;
        end_hour=0;
        end_minute=0;
    }

    /**
     * 初始化Toolbar
     */
    private void inittoobar() {
        tvTopbarMenu.setTextColor(Color.BLACK);
        tvTopbarMenu.setText("保存");
        toolbar.setTitle("编辑计划");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar.setElevation(12f);
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

    @Override
    protected void initdata() {
        plan = new EventModel();
        //初始化日期选择框
        datePicker = new DatePicker(this);
        datePicker.setOffset(1);
        //初始化时间选择框
        start_timePicker = new TimePicker(this);
        end_timepicker = new TimePicker(this);
        //今日
        now_month_day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        //现在月份
        now_month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        //当前年份
        now_year = Calendar.getInstance().get(Calendar.YEAR);
        datePicker.setRangeStart(now_year,now_month,now_month_day);
        datePicker.setRangeEnd(now_year,12,31);
        datePicker.setSelectedItem(now_year,now_month,now_month_day);
        datePicker.setLineSpaceMultiplier(4);
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_work_plan_edit;
    }

    /**
     * 上传回调
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void createPlan(UploadPlanEvent event) {
        if (event.isOk()) {
            SuccessBean successBean = event.getBean();
            if (successBean.getStatus() == 1) {
                //成功
                ToastUtils.showToast("上传成功");
                //本地保存一份
                //modelDao.save(plan);
                //结束当前页面
                finish();

            } else {
                //失败
                ToastUtils.showToast("上传失败");
            }
        } else {
            //ToastUtils.showToast(event.getCodeDescribe());
            finish();
        }
    }
}
